package org.dreamfly.netshop.manage;

import java.util.ArrayList;
import java.util.List;

import org.dreamfly.netshop.dao.GoodsInfoDao;
import org.dreamfly.netshop.entity.GoodsInfo;
import org.dreamfly.netshop.service.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.PropertyFilter.MatchType;
import org.springside.modules.orm.hibernate.EntityManager;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

/**
 * 用户管理类. 实现领域对象用户的所有业务管理函数. 演示派生DAO层子类的模式,由注入的UserDao封装数据库访问. 通过范型声明继承EntityManager,默认拥有CRUD管理方法. 使用Spring
 * annotation定义事务管理.
 * 
 * @author calvin
 */
// Spring Service Bean的标识.
@Service
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class GoodsInfoManager extends EntityManager<GoodsInfo, Long> {
    @Autowired
    private GoodsInfoDao goodsInfoDao;

    /**
     * 实现回调函数,为EntityManager基类的CRUD操作提供DAO.
     */
    @Override
    protected GoodsInfoDao getEntityDao() {
        return goodsInfoDao;
    }

    public List<GoodsInfo> getGoodsListByClass(long classId) {
        return goodsInfoDao.find("from GoodsInfo as o where o.ClassID=? order by o.addDate desc", classId);
    }

    public Page<GoodsInfo> getGoodsListByClass(long classId, Page<GoodsInfo> page) {
        return goodsInfoDao.find(page, "from GoodsInfo as o where o.type.id=? order by o.addDate desc", classId);
    }

    public List<GoodsInfo> getGoodsListByHot() {
        return goodsInfoDao.find("from GoodsInfo as o where o.hot=? order by o.addDate desc", true);
    }

    public Page<GoodsInfo> getGoodsListByHot(Page<GoodsInfo> page) {
        return goodsInfoDao.find(page, "from GoodsInfo as o where o.hot=? order by o.addDate desc", true);
    }

    public List<GoodsInfo> getGoodsListByRefinement() {
        return goodsInfoDao.find("from GoodsInfo as o where o.refinement=? order by o.addDate desc", true);
    }

    public Page<GoodsInfo> getGoodsListByRefinement(Page<GoodsInfo> page) {
        return goodsInfoDao.find(page, "from GoodsInfo as o where o.refinement=? order by o.addDate desc", true);
    }

    public List<GoodsInfo> getGoodsListByDiscount() {
        return goodsInfoDao.find("from GoodsInfo as o where o.discount=? order by o.addDate desc", true);
    }

    public Page<GoodsInfo> getGoodsListByDiscount(Page<GoodsInfo> page) {
        return goodsInfoDao.find(page, "from GoodsInfo as o where o.discount=? order by o.addDate desc", true);
    }

    public Page<GoodsInfo> getGoodsListByClassID(Page<GoodsInfo> page, long id) {
        return goodsInfoDao.find(page, "from GoodsInfo as o where o.class.id=? order by o.addDate desc", id, true);
    }

    public Page<GoodsInfo> getGoodsListBySupport(Page<GoodsInfo> page, String support) {
        return goodsInfoDao.find(page, "from GoodsInfo as o where o." + support + "=? order by o.addDate desc", true);
    }

    public GoodsInfo getDetailById(long id) {
        return goodsInfoDao.get(id);
    }

    public Page<GoodsInfo> search(Page<GoodsInfo> page, GoodsInfo goodInfo) {
        List<PropertyFilter> list = new ArrayList<PropertyFilter>();
        if (goodInfo.getGoodsName() != null)
            list.add(new PropertyFilter("goodsName", goodInfo.getGoodsName().trim(), MatchType.LIKE));
        if (goodInfo.getType() != null)
            list.add(new PropertyFilter("type", goodInfo.getType(), MatchType.LIKE));
        return goodsInfoDao.findByFilters(page, list);
    }

    /**
     * 重载delte函数,演示异常处理及用户行为日志.
     */
    @Override
    public void delete(Long id) {
        if (id == 1) {
            logger.warn("操作员{}尝试删除超级管理员用户", SpringSecurityUtils.getCurrentUserName());
            throw new ServiceException("不能删除超级管理员用户");
        }

        goodsInfoDao.delete(id);
    }

}
